import functions from "./functions.js";
import { getToken } from "@/utils/auth.js";
const apiUrl = `${functions.getDomain()}/uploads/manyUploadsFile`;

export default function(params) {
	
	return new Promise((relove, reject) => {
		let num = 6 - params.imgLength;
		console.log("params",params);
		console.log("num",num);
		uni.chooseImage({
			count: num, //默认9
			sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
			sourceType: ['album','camera'], //从相册选择
			success: function(res) {
				const tempFiles = res.tempFilePaths
				console.log("tempFilePaths",res);
				let imgs = tempFiles.map((value,index) =>{
					return{
						name:'files'+index,
						uri:value
					}
				})
				uni.showLoading({
				    title: '上传中',
					mask:true
				});
				uni.uploadFile({
					url: apiUrl,
					files: imgs,
					success: (opt) => {
						console.log("imgs",imgs);
						let data = JSON.parse(opt.data);
						uni.hideLoading();
						
						console.log("图片返回",data);
						if(data.code == 200){
							relove(data.data);
						}else{
							reject();
						}
						
					},
					fail: (err) => {
						uni.hideLoading();
						reject();
					}
				});

			}
		});

	})

}
